package com.example.ykprinteridcard;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yanke.printer.YKPrinterListener;
import com.example.yanke.printer.YKPrinterManager;
import com.example.yanke.printerc1.YKPrinterC1Listener;
import com.example.yanke.printerc1.YKPrinterC1Manager;
import com.szsicod.print.escpos.PrinterAPI;
import com.telpo.tps550.api.TelpoException;
import com.telpo.tps550.api.printer.UsbThermalPrinter;

import java.io.UnsupportedEncodingException;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created By kzz on 2021/8/16
 * Describe: 打印
 */
public class PrinterActivity extends AppCompatActivity {

    private EditText editText;
    private static String printContent1 = "终端编号：CL001\n" +
            "交易流水：201908122053320\n" +
            "供应商名称：【测试-供应商】自营-知更鸟供应商\n" +
            "分销商来源：知更鸟\n" +
            "\n" +
            "商品名称：打印测试\n" +
            "商品单价：1.0\n" +
            "证件号码：430224199003163618\n" +
            "凭证号码：99022260924977\n" +
            "本次消费总价：1.0\n" +
            "手 机 号：18665980929\n" +
            "打印时间：2019-08-12 20:53:32\n" +
            "\n" +
            "----------------------------\n" +
            "【测试-业务运营商】知更鸟5A景区欢迎您光临\n" +
            "----------------------------";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer);
        editText = findViewById(R.id.etContext);
//        mYKPrinterManager = new YKPrinterManager(this);
//        mYKPrinterC1Manager = new YKPrinterC1Manager(this);
    }

    public void printer(View view) {

        if (!TextUtils.isEmpty(editText.getText())) {
//            for (int i = 0; i < 10; i++) {
            YKPrinterManager.getInstance(this).initPrinter(new YKPrinterListener() {


                @Override
                public void initSuccess(UsbThermalPrinter mPrinter) throws Exception {
                    //                        try {
                    mPrinter.reset();
//                    mPrinter.walkPaper(15);
//                    mPrinter.setAlgin(UsbThermalPrinter.ALGIN_LEFT);
//                    mPrinter.setLeftIndent(leftDistance);//(左边距最大0-255)
//                    mPrinter.setLineSpace(lineDistance);//(行距距最大0-255)
//                    mPrinter.setBold(isBold);//是否粗体
//                    mPrinter.setTextSize(wordFont);//(字体大小8-64)

                    mPrinter.setGray(7);//打印黑色程度(0-7)

                    mPrinter.addString(editText.getText().toString());
                    mPrinter.printString();
                    mPrinter.walkPaper(10);
//                        } catch (TelpoException e) {
//                            e.printStackTrace();
//                        }
                }

                @Override
                public void initError(String msg) {
                    Toast.makeText(PrinterActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            });
//            }
        } else
            Toast.makeText(this, "请输入打印内容", Toast.LENGTH_SHORT).show();

    }

    public void printerC1(View view) {
        if (!TextUtils.isEmpty(editText.getText())) {
//            for (int i=0;i<10;i++) {
            YKPrinterC1Manager.getInstance(this).initPrinter(new YKPrinterC1Listener() {


                @Override
                public void initSuccess(PrinterAPI mPrinter)  {
                    mPrinter.init();
                    mPrinter.setFontStyle(0);
                    mPrinter.setAlignMode(UsbThermalPrinter.ALGIN_MIDDLE);
                    try {
                        mPrinter.printString(editText.getText().toString());
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    mPrinter.printAndFeedLine(5);
                }

                @Override
                public void initError(String msg) {
                    Toast.makeText(PrinterActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            });
//            }
        } else
            Toast.makeText(this, "请输入打印内容", Toast.LENGTH_SHORT).show();
    }

    public void testText(View view) {
        editText.setText("");
        editText.setText(printContent1);
    }

    public void clearText(View view) {
        editText.setText("");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        YKPrinterManager.getInstance(this).releasePrint();
        YKPrinterC1Manager.getInstance(this).releasePrint();
    }
}
